package com.maxd.respository.helpTask;

import com.maxd.model.HelpLuckyValue;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import java.util.Map;

public interface HelpLuckyValueRepository extends Repository<HelpLuckyValue, Long> {


    @Query(value = "select * from help_lucky_value where user_id=:userId",nativeQuery = true)
    HelpLuckyValue selectByUserId(@Param("userId") Long userId);

    @Modifying
    @Query(value = "update help_lucky_value set lucky_value=lucky_value+:luckyValue where user_id=:userId",nativeQuery = true)
    int updateHelpLuckyValue(@Param("userId") Long userId,@Param("luckyValue") Double luckyValue);


    @Modifying
    @Query(value = "update help_lucky_value set accruing_amounts=accruing_amounts+:accruingAmounts where user_id=:userId",nativeQuery = true)
    int updateHelpLuckyValueAccruingAmounts(@Param("userId") Long userId,@Param("accruingAmounts") Double accruingAmounts);


    @Query(value = "SELECT u.nick_name,vq.user_id,q.`name`,vq.state ,vq.create_time " +
            "FROM lucky_value_queue vq " +
            "LEFT JOIN lucky_queue q ON vq.lucky_queue_id = q.id " +
            "LEFT JOIN user_info u ON u.id = vq.user_id " +
            "where vq.lucky_queue_id=:luckyQueueId " +
            "order by vq.create_time ",nativeQuery = true)
    Page<Map<String,Object>> selectLuckyQueueRanking(Pageable pageable,@Param("luckyQueueId") Long luckyQueueId);

    @Query(value = "SELECT u.nick_name,vq.user_id,q.`name`,vq.state ,vq.create_time " +
            "FROM lucky_value_queue vq " +
            "LEFT JOIN lucky_queue q ON vq.lucky_queue_id = q.id " +
            "LEFT JOIN user_info u ON u.id = vq.user_id " +
            "where vq.lucky_queue_id=:luckyQueueId and vq.user_id in (select id from user_info where phone=:phone) " +
            "order by vq.create_time ",nativeQuery = true)
    Page<Map<String,Object>> selectLuckyQueueRanking(Pageable pageable,@Param("luckyQueueId") Long luckyQueueId,@Param("phone") String phone);





}
